package com.pig.controller;

import com.pig.dto.CartItemDTO;
import com.pig.entity.CartItem;
import com.pig.service.CartService;
import com.pig.dto.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/cart")
@Tag(name = "购物车管理", description = "购物车相关接口")
public class CartController {
    
    @Autowired
    private CartService cartService;
    
    @PostMapping("/add")
    @Operation(summary = "添加商品到购物车")
    public Result<CartItem> addToCart(@RequestParam Long userId, @RequestBody CartItemDTO cartItemDTO) {
        try {
            CartItem cartItem = cartService.addToCart(userId, cartItemDTO);
            return Result.success(cartItem);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @GetMapping("/list")
    @Operation(summary = "获取用户购物车列表")
    public Result<List<CartItem>> getCartItems(@RequestParam Long userId) {
        try {
            List<CartItem> cartItems = cartService.getCartItems(userId);
            return Result.success(cartItems);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @PutMapping("/{cartItemId}/quantity")
    @Operation(summary = "更新购物车商品数量")
    public Result<CartItem> updateQuantity(@RequestParam Long userId, 
                                         @PathVariable Long cartItemId, 
                                         @RequestParam Integer quantity) {
        try {
            CartItem cartItem = cartService.updateQuantity(userId, cartItemId, quantity);
            return Result.success(cartItem);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @DeleteMapping("/{cartItemId}")
    @Operation(summary = "删除购物车商品")
    public Result<Boolean> removeFromCart(@RequestParam Long userId, @PathVariable Long cartItemId) {
        try {
            boolean result = cartService.removeFromCart(userId, cartItemId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @DeleteMapping("/clear")
    @Operation(summary = "清空用户购物车")
    public Result<Boolean> clearCart(@RequestParam Long userId) {
        try {
            boolean result = cartService.clearCart(userId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @GetMapping("/count")
    @Operation(summary = "获取购物车商品总数")
    public Result<Integer> getCartItemCount(@RequestParam Long userId) {
        try {
            int count = cartService.getCartItemCount(userId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
} 